// packageC/selfCarry/selectSelfPickupPoint/selectSelfPickupPoint.js
const app = getApp();
var location = require("../../../mybehaviors/location/location");
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    positongStatus: 0, //是否正在定位状态0、定位成功1、定位失败-1
    showArea: false,
    showPopover: false,
    activeActions: "地址",
    // 通过 actions 属性来定义菜单选项
    actions: [{
      text: "地址",
      value: 1
    }, {
      text: "自提点",
      value: 2
    }],

    latitude: 0,
    longitude: 0,

    plugin_name: "",
    activeID: 0, //当前选择的自提点id
    activeDeliver: {},
    list: [],
    kwd: "",
    search_address: "",
    city: "",
    activeActionsVaule: 1,

    //more
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._getLocation((mapdata, e) => {
      const latitude = e.lat;
      const longitude = e.lng;
      this.setData({
        longitude,
        latitude,
        city: !app._isTextEmpty(mapdata.address_component.city) ? mapdata.address_component.city : mapdata.address_component.province,
        positongStatus: 1
      });
      this.getNowPackageDeliver();
      this.getList();
    });

  },
  getNowPackageDeliver() {
    //通过定位信息获取自提点信息
    let _json = {
      lng: this.data.longitude||'',
      lat: this.data.latitude||''
    };
    let deliverIDStorage = null;
    try {
      deliverIDStorage = wx.getStorageSync('deliverId'); //获取当前会话是否存储了自提点id
    } catch (e) {
      // Do something when catch error
    }

    if (deliverIDStorage) {
      _json.deliver_id = deliverIDStorage;
    }

    let urlStr = app.getNetAddresss('plugin.package-deliver.frontend.decorate-deliver.getNowPackageDeliver');
    app._postNetWork({
      url: urlStr,
      data: _json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            activeDeliver: res.data
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });

        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  getList() {
    let _json = {
      lng: this.data.longitude,
      lat: this.data.latitude,
      city_name: this.data.city,
      kwd: this.data.kwd,
      search_address: this.data.search_address
    };
    let urlStr = app.getNetAddresss('plugin.package-deliver.frontend.decorate-deliver.getList');
    app._postNetWork({
      url: urlStr,
      data: _json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            list: res.data.list.data,
            plugin_name: res.data.plugin_name,
            'actions[1].text': res.data.plugin_name,
            total_page: res.data.list.last_page
          });
          wx.setNavigationBarTitle({
            title: '选择' + res.data.plugin_name
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });

        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });

  },
  chooseDeliver(e) {
    let urlStr = app.getNetAddresss('plugin.package-deliver.frontend.decorate-deliver.choosePackageDeliver');
    app._postNetWork({
      url: urlStr,
      data: {
        deliver_id: e
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          wx.setStorageSync("deliverId", e.detail);
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.msg,
            success(res) {
              try {
                wx.navigateBack({
                  delta: 1,
                  fail:()=>{
                    wx.reLaunch({
                      url: "/packageG/index/index",
                    });
                  }
                });
              } catch (error) {
                console.log(error);
              }
            }
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });

        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },

  tapKwd(e) {
    this.setData({
      kwd: e.detail.value
    });
  },

  tapAddress(e) {
    this.setData({
      search_address: e.detail.value
    });
  },

  showAddress() {
    this.setData({
      showArea: true
    });
  },
  onShowPopver() {
    let showPopover = !this.data.showPopover;
    this.setData({
      showPopover: showPopover
    });
  },
  onSelectPopver(e) {
    let _item = e.currentTarget.dataset.item;
    this.setData({
      activeActions: _item.text,
      activeActionsVaule: _item.value,
      kwd: '',
      search_address: '',
      showPopover: false
    });
    this.getList();
  },
  confirmArea(e) {
    this.setData({
      positongStatus: 1,
      city: e.detail[1].name
    });
    this.getList();
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});